package com.lp.service.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.iframework.commons.domain.enums.RecordStatus;
import org.iframework.commons.util.fast.L;
import org.iframework.commons.util.fast.V;
import org.iframework.support.spring.controller.BaseControllerSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.lp.business.domain.Result;
import com.lp.business.model.Industry;
import com.lp.business.model.Member;
import com.lp.business.service.IndustryService;
import com.lp.business.service.MemberService;

/**
 * 行业信息
 * 
 * @author lipeng
 * @date 2018年08月22日 下午1:15:45
 */
@RestController
@RequestMapping("/api/industry")
public class IndustryController extends BaseControllerSupport {

	@Autowired
	private IndustryService industryService;
	@Autowired
	private MemberService memberService;
	
	/**
	 * 获取行业列表 <br>
	 */
	@RequestMapping(value = "list", method = { RequestMethod.POST, RequestMethod.GET })
	public Result list(HttpServletRequest request, final HttpServletResponse response) {
		try {
			Industry model = new Industry();
			model.setStatus(RecordStatus.AVAILABLE);
			
			List<Map<String, Object>> industries = industryService.findIndustrys(model);
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("industries", industries);
			
			return Result.ok().put("data", data);
		} catch (Exception e) {
			L.e(e.getMessage(), e);
			return Result.error("接口异常");
		}
	}
	
	/**
	 * 获取行业明细 <br>
	 */
	@RequestMapping(value = "detail/{id}", method = { RequestMethod.POST, RequestMethod.GET })
	public Result detail(HttpServletRequest request, final HttpServletResponse response, @PathVariable String id) {
		try {
			String openId = (String)request.getAttribute("openId");
			if(V.isEmpty(openId)) {
				return Result.error("非法的token");
			}
			Member member = memberService.findByOpenId(openId);
			if(V.isEmpty(member)) {
				return Result.error("非法的token");
			}
			
			if(V.isEmpty(id)) {
				return Result.error("获取行业明细失败");
			}
			Industry industry = industryService.get(id);
			if(V.isEmpty(industry)) {
				return Result.error("获取行业明细失败");
			}
			
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("industry", industry);
			return Result.ok().put("data", data);
		} catch (Exception e) {
			L.e(e.getMessage(), e);
			return Result.error("接口异常");
		}
	}
	
	
}
